function [H] = LambdaToH_3_Extended(lambda,VarTypeInd, nnonmonthly)
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here


n = size(VarTypeInd,1);
nf = size(lambda,2)/(5+1);
nftot = nf+nnonmonthly;

% Enlarge Lambda to Account for Idiosyncratic Components of Non-Monthly
% Variables

lambda_expanded = zeros(n,(nftot)*(5+1));
lambda_idiosyncratic = zeros(n,nnonmonthly);
if nnonmonthly > 0
lambda_idiosyncratic(VarTypeInd>99,:) = eye(nnonmonthly);
else lambda_idiosyncratic = nan(nftot,0); 
end

for jj = 1:(5+1)
    
    lambda_expanded(:,1+(jj-1)*(nf+nnonmonthly):nf+(jj-1)*(nftot)) = lambda(:,(jj-1)*nf+1:nf+(jj-1)*nf);
        
end

    lambda_expanded(:,1+nf:nf+nnonmonthly) = lambda_idiosyncratic(:,1:nnonmonthly);


clear BigLambda H
for ii = 1:n
    clear Hi
    if VarTypeInd(ii,1)==100
        Hi = (1/4)*(1/12)*[[1:12] [11:-1:1]];
    elseif VarTypeInd(ii,1)==150
        Hi = (1/12)*[ones(1,12) zeros(1,11)];
    elseif VarTypeInd(ii,1)==200
        Hi = [1/12 2/12 (3/12)*ones(1,10) 2/12 1/12 zeros(1,9)];
    elseif VarTypeInd(ii,1)==300
        Hi = [1/3 2/3 3/3 2/3 1/3 zeros(1,18)];
    elseif VarTypeInd(ii,1)==3
        Hi = [1/3 1/3 1/3 zeros(1,20)];
    elseif VarTypeInd(ii,1)==6
        Hi = [ones(1,6) zeros(1,17)];
    elseif VarTypeInd(ii,1)==12
        Hi = [ones(1,12) zeros(1,11)];
    else
        Hi = [1 zeros(1,22)];
    end
        clear HH 
        HH = [];
        for jj=0:5
            HH=[HH zeros(size(HH,1),1); zeros(1,jj) Hi];
        end
        for jj=0:5
            lambda_iis = lambda_expanded(ii,nftot*(jj+1-1)+1:nftot*(jj+1));
            Lambda_by_HH(jj+1,:) = kron(HH(jj+1,:),lambda_iis);
        end
        Lambda_ii = sum(Lambda_by_HH,1);
        clear Lambda_by_HH
        BigLambda(ii,:) = Lambda_ii;
end

if sum((VarTypeInd==100))==0 && sum((VarTypeInd==12))>0
    H = BigLambda(:,1:end-11*nftot);
elseif sum((VarTypeInd==100))==0 && sum((VarTypeInd==12))==0
    vvv= cumsum(sum(BigLambda,1)~=0); 
    finalCol = find(vvv == max(vvv),1,'first');
    H = BigLambda(:,1:finalCol);
else 
    H = BigLambda;
end

end



